package com.moli.iov.protocol.util;

import com.moli.iov.protocol.enums.CommonEnableTag;
import com.moli.iov.protocol.enums.CommonLockTag;
import com.moli.iov.protocol.enums.CommonOpenTag;
import com.moli.iov.protocol.enums.CommonSwitchTag;
import com.moli.iov.protocol.enums.extend.*;
import com.moli.iov.protocol.enums.realtime.*;
//import com.moli.iov.protocol.model.dto.realtime.VehicleGpsDto;
import com.moli.iov.protocol.model.dto.realtime.ExtendVehicleStateDto;
import com.moli.iov.protocol.model.dto.realtime.VehicleAllDto;
import com.moli.iov.protocol.model.dto.realtime.VehicleGpsDto;
import com.moli.iov.protocol.model.realtime.*;
import com.moli.iov.util.DateUtil;
//import com.moli.iov.protocol.model.dto.realtime.*;

import java.util.List;

public class RealTimeInfoUtil {

    public static final String NULL = "--";

    public static final byte DEFAULT_BYTE = 0;

    public static final double DEFAULT_DOUBLE = 0;

    public static final int DEFAULT_INT = 0;

    public static final int DEFAULT_SHORT = 0;


    public static String getLastOdoReportTime(RealTimeInfo info){
        if(info!=null){
            if(info.getLastOdoReportTime()!=null) {
                return DateUtil.format(DateUtil.YEAR_SECOND, info.getLastOdoReportTime());
            }else{
                if(info.getRealTimeInfoReq()!=null && info.getRealTimeInfoReq().getCollectDate()!=null){
                    return DateUtil.format(DateUtil.YEAR_SECOND, info.getRealTimeInfoReq().getCollectDate().getDate());
                }
            }
        }
        return NULL;
    }

    /**
     * 根据车辆实时数据判断车辆是否定位
     */
    public static Boolean getLocationStatus(VehicleGpsReq dto) {
        if (dto == null || dto.getLocationState() == null) {
            return false;
        }
        LocationStateTag locationState = dto.getLocationState();
        if (locationState.getIsValid() != null
                && locationState.getIsValid() == LocationStateTag.IsValid.YES) {
            return true;
        } else {
            return false;
        }
    }

    public static double getGpsOdo(RealTimeInfo info){
        if(info!=null && info.getGpsOdo()!=null){
            return info.getGpsOdo();
        }
        return DEFAULT_DOUBLE;
    }

    public static String getLocationStatusK(VehicleGpsReq vehicleGpsDto) {
        if (vehicleGpsDto == null || vehicleGpsDto.getLocationState() == null) {
            return NULL;
        }
        LocationStateTag locationState = vehicleGpsDto.getLocationState();
        if (locationState != null && locationState.getIsValid() != null) {
            return locationState.getIsValid().getKey();
        }
        return NULL;
    }

    public static Boolean getLocationStatus(LocationStateTag tag) {
        if (tag == null || tag.getIsValid() == null) {
            return false;
        }

        if(tag.getIsValid()!=null && tag.getIsValid().getValue()==0){
            return true;
        } else {
            return false;
        }
    }

    public static String getVehicleStateKey(VehicleAllReq vehicleAll) {
        if (vehicleAll != null) {
            VehicleStateTag vehicleState = vehicleAll.getVehicleState();
            if (vehicleState != null) {
                return vehicleState.getK();
            }

        }
        return NULL;
    }

//    public static String getPowerGear(RealTimeInfoDto realTimeInfo) {
//
//        ExtendVehicleState vehicleState = realTimeInfo.getExtendVehicleState();
//        if (vehicleState != null) {
//            ExtendPowerGearTag powerGear = vehicleState.getExtendPowerGear();
//            if (powerGear != null) {
//                return powerGear.getK();
//            }
//        }
//        return NULL;
//    }

    public static String getPowerGear(ExtendVehicleState vehicleState) {
        if (vehicleState != null) {
            ExtendPowerGearTag powerGear = vehicleState.getExtendPowerGear();
            if (powerGear != null) {
                return powerGear.getK();
            }
        }
        return NULL;
    }

//    public static String getGear(RealTimeInfoDto realTimeInfo) {
//        VehicleAllReq vehicleAll = realTimeInfo.getVehicleAll();
//        if (vehicleAll != null) {
//            GearStateTag gear = vehicleAll.getGear();
//            if (gear != null) {
//                return gear.getK();
//            }
//
//        }
//        return NULL;
//    }

    public static String getGear(VehicleAllReq vehicleAll) {
        if (vehicleAll != null) {
            GearStateTag gear = vehicleAll.getGear();
            if (gear != null) {
                return gear.getK();
            }

        }
        return NULL;
    }

    public static String getLeasingStatus(ExtendVehicleState dto) {
        if (dto != null) {
            ExtendLeasingModelTag leasing = dto.getExtendLeasing();
            if (leasing != null) {
                return leasing.getK();
            }
        }
        return NULL;
    }

    public static int getGpsOde(ExtendVehicleState dto) {
        if (dto != null) {
            Integer gpsOde = dto.getExtendGpsOde();
            if (gpsOde != null) {
                return gpsOde;
            }
        }
        return DEFAULT_INT;
    }

    public static int getGpsOde(RealTimeInfo info) {
//        if(info!=null) {
//            Integer odo = 0;
//            if(info.getGpsOdoList()!=null){
//                for (Integer integer : info.getGpsOdoList()) {
//                    odo+=integer;
//                }
//            }
//            if(info.getRealTimeInfoReq()!=null
//                    &&info.getRealTimeInfoReq().getExtendReq()!=null
//                    && info.getRealTimeInfoReq().getExtendReq().getextendVehicleState()!=null){
//                odo += info.getRealTimeInfoReq().getExtendReq().getextendVehicleState().getExtendGpsOde();
//            }
//            return odo;
//        }
//        return DEFAULT_INT;

        if(info!=null){
            if(info.getGpsOdo()!=null){
                return (int)Math.ceil(info.getGpsOdo());
            }else{
                if(info.getRealTimeInfoReq()!=null && info.getRealTimeInfoReq().getVehicleAllReq()!=null ){
                    return (int)Math.ceil(info.getRealTimeInfoReq().getVehicleAllReq().getOdometer());
                }
            }
        }
        return DEFAULT_INT;
    }

    public static String getKeyStatus(ExtendVehicleState dto) {
        if (dto != null) {
            CommonEnableTag tag = dto.getPhysicalKeyState();
            if (tag != null) {
                return tag.getK();
            }
        }
        return NULL;
    }

    public static String getDynamicStatus(ExtendVehicleState dto) {
        if (dto != null) {
            CommonLockTag tag = dto.getDynamicState();
            if (tag != null) {
                return tag.getK();
            }
        }
        return NULL;
    }

    public static String getChargeStatus(VehicleAllReq vehicleAllDto) {
        if (vehicleAllDto != null) {
            ChargeStateTag chargeState = vehicleAllDto.getChargeState();
            if (chargeState != null) {
                return chargeState.getK();
            }
        }
        return NULL;
    }

    public static double getSpeed(VehicleAllReq vehicleAllDto) {
        if (vehicleAllDto != null) {
            double result = vehicleAllDto.getSpeed();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_DOUBLE;
    }


    public static double getOdometer(VehicleAllReq vehicleAllDto) {
        if (vehicleAllDto != null) {
            double odo = vehicleAllDto.getOdometer();
            if (isValid(odo)) {
                return odo;
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static byte getSpeedUpValue(VehicleAllReq vehicleAllDto) {
        if (vehicleAllDto != null) {
            byte result = vehicleAllDto.getSpeedUpValue();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_BYTE;
    }

    public static byte getBrakeTravelValue(VehicleAllReq vehicleAllDto) {
        if (vehicleAllDto != null) {
            byte result = vehicleAllDto.getBrakeTravelValue();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_BYTE;
    }

    public static double getTotalVoltage(VehicleAllReq vehicleAllDto) {
        if (vehicleAllDto != null) {
            double result = vehicleAllDto.getTotalVoltage();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_DOUBLE;
    }

    private static Boolean isValid(double result) {
        if (result != -98 && result != -99) {
            return true;
        } else {
            return false;
        }
    }

    private static Boolean isValid(int result) {
        if (result != -98 && result != -99) {
            return true;
        } else {
            return false;
        }
    }

    private static Boolean isValid(short result) {
        if (result != -98 && result != -99) {
            return true;
        } else {
            return false;
        }
    }

    private static Boolean isValid(byte result) {
        if (result != -98 && result != -99) {
            return true;
        } else {
            return false;
        }
    }

    public static double getTotalCurrent(VehicleAllReq vehicleAllDto) {
        if (vehicleAllDto != null) {
            double result = vehicleAllDto.getTotalCurrent();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static String getDcStatus(VehicleAllReq dto) {
        if (dto != null) {
            DcStateTag leasing = dto.getDcStateTag();
            if (leasing != null) {
                return leasing.getK();
            }
        }
        return NULL;
    }

    public static int getInsResistance(VehicleAllReq vehicleAllDto) {
        if (vehicleAllDto != null) {
            int result = vehicleAllDto.getInsResistance();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_INT;
    }

    public static int getExtendGpsSpeed(ExtendVehicleState stateDto) {
        if (stateDto != null) {
            int result = stateDto.getExtendGpsSpeed();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_INT;
    }

    public static int getExtendGpsDirection(ExtendVehicleState stateDto) {
        if (stateDto != null) {
            int result = stateDto.getExtendGpsDirection();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_INT;
    }

    public static int getExtendDeviceLocationCount(ExtendVehicleState stateDto) {
        if (stateDto != null) {
            int result = stateDto.getExtendDeviceLocationCount();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_INT;
    }

    public static int getExtendSignalIntensity(ExtendVehicleState stateDto) {
        if (stateDto != null) {
            int result = stateDto.getExtendSignalIntensity();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_INT;
    }

    public static String getPhysicalKeyStatus(ExtendVehicleState dto) {
        if (dto != null) {
            CommonEnableTag result = dto.getPhysicalKeyState();
            if (result != null) {
                return result.getK();
            }
        }
        return NULL;
    }

    public static String getUsbInternetStatus(ExtendVehicleState dto) {
        if (dto != null) {
            CommonOpenTag result = dto.getUsbInternetState();
            if (result != null) {
                return result.getK();
            }
        }
        return NULL;
    }

    public static String getBluetoothStatus(ExtendVehicleState dto) {
        if (dto != null) {
            CommonLockTag result = dto.getBluetoothState();
            if (result != null) {
                return result.getK();
            }
        }
        return NULL;
    }

    public static double getExtendBatteryVoltage(ExtendVehicleState stateDto) {
        if (stateDto != null) {
            double result = stateDto.getExtendBatteryVoltage();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static String getDoorStatus(ExtendVehicleState stateDto) {
        if (stateDto != null) {

            if(stateDto.getExtendDoorState()==0xFF ||stateDto.getExtendDoorState()==0xFE
                    ||stateDto.getExtendDoorState()==-98
                    ||stateDto.getExtendDoorState()==-99){
                return "--";
            }else{
                CommonSwitchTag rearDoor = stateDto.getRearDoor();
                CommonSwitchTag leftBackDoor = stateDto.getLeftBackDoor();
                CommonSwitchTag rightBackDoor = stateDto.getRightBackDoor();
                CommonSwitchTag leftFrontDoor = stateDto.getLeftFrontDoor();
                CommonSwitchTag rightFrontDoor = stateDto.getRightFrontDoor();

                StringBuilder sb = new StringBuilder();
                sb.append("左前门:");
                sb.append(leftFrontDoor.getK());
                sb.append(",右前门:");
                sb.append(rightFrontDoor.getK());
                sb.append(",左后门:");
                sb.append(leftBackDoor.getK());
                sb.append(",右后门:");
                sb.append(rightBackDoor.getK());
                sb.append(",后备门:");
                sb.append(rearDoor.getK());

                return sb.toString();
            }


        }
        return NULL;
    }

    public static String getWindowStatus(ExtendVehicleState stateDto) {
        if (stateDto != null) {

            if(stateDto.getExtendWindowState()==0xFF ||stateDto.getExtendWindowState()==0xFE
                    ||stateDto.getExtendWindowState()==-98
                    ||stateDto.getExtendWindowState()==-99){
                return "--";
            }else{
                CommonSwitchTag leftBackWindow = stateDto.getLeftBackWindow();
                CommonSwitchTag leftFrontWindow = stateDto.getLeftFrontWindow();
                CommonSwitchTag rightBackWindow = stateDto.getRightBackWindow();
                CommonSwitchTag rightFrontWindow = stateDto.getRightFrontWindow();

                StringBuilder sb = new StringBuilder();
                sb.append("左前窗:");
                sb.append(leftFrontWindow.getK());
                sb.append(",右前窗:");
                sb.append(rightFrontWindow.getK());
                sb.append(",左后窗:");
                sb.append(leftBackWindow.getK());
                sb.append(",右后窗:");
                sb.append(rightBackWindow.getK());

                return sb.toString();
            }


        }
        return NULL;
    }

    public static String getLampStatus(ExtendVehicleState stateDto) {

        if (stateDto != null) {
            if(stateDto.getExtendLightState()==0xFF ||stateDto.getExtendLightState()==0xFE||
                stateDto.getExtendLightState()==-98 ||stateDto.getExtendLightState()==-99){
                return "--";
            }else{
                CommonSwitchTag highBeam = stateDto.getHighBeam();
                CommonSwitchTag lowBeam = stateDto.getLowBeam();
                CommonSwitchTag readingLamp = stateDto.getReadingLamp();
                CommonSwitchTag frontFogLamp = stateDto.getFrontFogLamp();
                CommonSwitchTag rearFogLamp = stateDto.getRearFogLamp();

                StringBuilder sb = new StringBuilder();
                sb.append("远光:");
                sb.append(highBeam.getK());
                sb.append(",近光:");
                sb.append(lowBeam.getK());
                sb.append(",前雾灯:");
                sb.append(frontFogLamp.getK());
                sb.append(",后雾灯:");
                sb.append(rearFogLamp.getK());
                sb.append(",阅读灯:");
                sb.append(readingLamp.getK());

                return sb.toString();
            }
        }
        return NULL;
    }



    public static String getHandBrakeStatus(ExtendVehicleState stateDto) {

        if (stateDto != null) {
            ExtendHandBrakeStateTag result = stateDto.getExtendHandBrakeState();
            if (result != null) {
                return result.getK();
            }
        }
        return NULL;
    }

    public static String getExtendControlLockState(ExtendVehicleState stateDto) {
        if (stateDto != null) {
            ExtendControlLockStateTag result = stateDto.getExtendControlLockState();
            if (result != null) {
                return result.getK();
            }
        }
        return NULL;
    }

    public static short getMotorSerialNum(DriveMotorReq driveMotor) {
        if (driveMotor != null) {
            List<DriveMotorInfo> list = driveMotor.getListDriveMotors();
            if (list != null && list.size() > 0) {
                DriveMotorInfo motorInfo = list.get(0);
                if (motorInfo != null) {
                    return motorInfo.getMotorSerialNum();
                }
            }
        }
        return DEFAULT_SHORT;
    }

    public static short getMotorControlTemperature(DriveMotorReq driveMotor) {

        if (driveMotor != null) {
            List<DriveMotorInfo> list = driveMotor.getListDriveMotors();
            if (list != null && list.size() > 0) {
                DriveMotorInfo motorInfo = list.get(0);
                if (motorInfo != null) {
                    short result = motorInfo.getMotorControlTemperature();
                    if (isValid(result)) {
                        return result;
                    }
                }
            }
        }
        return DEFAULT_SHORT;
    }

    public static int getMotorRotateSpeed(DriveMotorReq driveMotor) {

            if (driveMotor != null) {
                List<DriveMotorInfo> list = driveMotor.getListDriveMotors();
                if (list != null && list.size() > 0) {
                    DriveMotorInfo motorInfo = list.get(0);
                    if (motorInfo != null) {
                        int result = motorInfo.getMotorRotateSpeed();
                        if (isValid(result)) {
                            return result;
                        }
                    }
            }
        }
        return DEFAULT_INT;
    }

    public static double getMotorControlVoltage(DriveMotorReq driveMotor) {
            if (driveMotor != null) {
                List<DriveMotorInfo> list = driveMotor.getListDriveMotors();
                if (list != null && list.size() > 0) {
                    DriveMotorInfo motorInfo = list.get(0);
                    if (motorInfo != null) {
                        double result = motorInfo.getMotorControlVoltage();
                        if (isValid(result)) {
                            return result;
                        }
                }
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static String getMotorStatus(DriveMotorReq driveMotor ) {
            if (driveMotor != null) {
                List<DriveMotorInfo> list = driveMotor.getListDriveMotors();
                if (list != null && list.size() > 0) {
                    DriveMotorInfo motorInfo = list.get(0);
                    if (motorInfo != null) {
                        DriveMotorStateTag motorState = motorInfo.getMotorState();
                        if (motorState != null) {
                            motorState.getK();
                        }
                }
            }
        }
        return NULL;
    }

    public static short getMotorTemperature(DriveMotorReq driveMotor) {
            if (driveMotor != null) {
                List<DriveMotorInfo> list = driveMotor.getListDriveMotors();
                if (list != null && list.size() > 0) {
                    DriveMotorInfo motorInfo = list.get(0);
                    if (motorInfo != null) {
                        short result = motorInfo.getMotorTemperature();
                        if (isValid(result)) {
                            return result;
                        }
                }
            }
        }
        return DEFAULT_SHORT;
    }

    public static double getMotorTorque(DriveMotorReq driveMotor) {
            if (driveMotor != null) {
                List<DriveMotorInfo> list = driveMotor.getListDriveMotors();
                if (list != null && list.size() > 0) {
                    DriveMotorInfo motorInfo = list.get(0);
                    if (motorInfo != null) {
                        double result = motorInfo.getMotorTorque();
                        if (isValid(result)) {
                            return result;
                        }
                }
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static double getMotorControlCurrent(DriveMotorReq driveMotor) {
            if (driveMotor != null) {
                List<DriveMotorInfo> list = driveMotor.getListDriveMotors();
                if (list != null && list.size() > 0) {
                    DriveMotorInfo motorInfo = list.get(0);
                    if (motorInfo != null) {
                        double result = motorInfo.getMotorControlCurrent();
                        if (isValid(result)) {
                            return result;
                        }
                }
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static double getLatitude(VehicleGpsReq vehicleGps) {
        if (vehicleGps != null) {
            if (isValid(vehicleGps.getLatitude())) {
                return vehicleGps.getLatitude();
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static double getLongitude(VehicleGpsReq vehicleGps) {
        if (vehicleGps != null) {
            if (isValid(vehicleGps.getLongitude())) {
                return vehicleGps.getLongitude();
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static int getGpsDirection(ExtendVehicleState extendVehicleState) {
        if (extendVehicleState != null) {
            if (isValid(extendVehicleState.getExtendGpsDirection())) {
                return extendVehicleState.getExtendGpsDirection();
            }
        }
        return DEFAULT_INT;
    }

    public static String getChargingGunState(ExtendVehicleState extendVehicleState) {
        if (extendVehicleState != null) {
            ExtendChargingGunStateTag res = extendVehicleState.getExtendChargingGunState();
            if(res!=null){
                return res.getK();
            }
        }
        return "--";
    }

    public static byte getSoc(VehicleAllReq vehicleAll) {
        if (vehicleAll != null) {
            byte soc = vehicleAll.getSoc();
            if (isValid(soc)) {
                return soc;
            }

        }
        return DEFAULT_BYTE;
    }

    public static String getRunModel(VehicleAllReq allDto) {
        if(allDto!=null){
            RunModeTag result = allDto.getRunMode();
            if (result != null) {
                return result.getK();
            }
        }
        return "--";
    }

    public static String getGear(VehicleAllDto vehicleAll) {
        if (vehicleAll != null) {
            GearStateTag gear = vehicleAll.getGear();
            if (gear != null) {
                return gear.getK();
            }

        }
        return NULL;
    }

    public static double getSpeed(VehicleAllDto vehicleAll) {
        if (vehicleAll != null) {
            double result = vehicleAll.getSpeed();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static double getLongitude(VehicleGpsDto vehicleGps) {
        if (vehicleGps != null) {
            if (isValid(vehicleGps.getLongitude())) {
                return vehicleGps.getLongitude();
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static double getLatitude(VehicleGpsDto vehicleGps) {
        if (vehicleGps != null) {
            if (isValid(vehicleGps.getLatitude())) {
                return vehicleGps.getLatitude();
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static byte getSoc(VehicleAllDto vehicleAll) {
        if (vehicleAll != null) {
            byte soc = vehicleAll.getSoc();
            if (isValid(soc)) {
                return soc;
            }

        }
        return DEFAULT_BYTE;
    }

    public static int getGpsDirection(ExtendVehicleStateDto extendVehicleState) {
        if (extendVehicleState != null) {
            if (isValid(extendVehicleState.getExtendGpsDirection())) {
                return extendVehicleState.getExtendGpsDirection();
            }
        }
        return DEFAULT_INT;
    }

    public static String getPowerGear(ExtendVehicleStateDto extendVehicleState) {
        if (extendVehicleState != null) {
            ExtendPowerGearTag powerGear = extendVehicleState.getExtendPowerGear();
            if (powerGear != null) {
                return powerGear.getK();
            }
        }
        return NULL;
    }

    public static String getLocationStatusK(VehicleGpsDto vehicleGps) {
        if (vehicleGps == null || vehicleGps.getLocationState() == null) {
            return NULL;
        }
        LocationStateTag locationState = vehicleGps.getLocationState();
        if (locationState != null && locationState.getIsValid() != null) {
            return locationState.getIsValid().getKey();
        }
        return NULL;
    }

    public static String getDynamicStatus(ExtendVehicleStateDto dto) {
        if (dto != null) {
            CommonLockTag tag = dto.getDynamicState();
            if (tag != null) {
                return tag.getK();
            }
        }
        return NULL;
    }

    public static double getOdometer(VehicleAllDto vehicleAll) {
        if (vehicleAll != null) {
            double odo = vehicleAll.getOdometer();
            if (isValid(odo)) {
                return odo;
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static double getExtendBatteryVoltage(ExtendVehicleStateDto stateDto) {
        if (stateDto != null) {
            double result = stateDto.getExtendBatteryVoltage();
            if (isValid(result)) {
                return result;
            }
        }
        return DEFAULT_DOUBLE;
    }

    public static String getLeasingStatus(ExtendVehicleStateDto dto) {
        if (dto != null) {
            ExtendLeasingModelTag leasing = dto.getExtendLeasing();
            if (leasing != null) {
                return leasing.getK();
            }
        }
        return NULL;
    }

    public static String getPhysicalKeyStatus(ExtendVehicleStateDto dto) {
        if (dto != null) {
            CommonEnableTag result = dto.getPhysicalKeyState();
            if (result != null) {
                return result.getK();
            }
        }
        return NULL;
    }
}
